using System;
using Gj.Galaxy.Logic;
using UnityEngine;

namespace Gj
{
    public enum MapKey : byte
    {
        Number,
        Random,
        Type,
        X,
        Y
    }

    public class MapPlugin : GamePlugin
    {
        public void OnInit(BaseGameSpace space)
        {
        }

        public void OnDestroy()
        {
        }

        public GameService[] GetRequireService()
        {
            return new GameService[] { CacheService.single, ObjectService.single, TerrainService.single };
        }

        public void OnUpdate()
        {
        }

        public void LateUpdate()
        {
        }

        public void OnProcessRoom(object[] data)
        {
            // 处理进程
            foreach (object[] detail in data)
            {
                var key = (InfoKey)detail[(byte)DetailKey.Key];
                switch (key)
                {
                    case InfoKey.MapKey:
                        var mapData = (object[])detail[(byte)DetailKey.Value];
                        var number = Convert.ToInt32(mapData[(byte)MapKey.Number]);
                        var random = Convert.ToInt32(mapData[(byte)MapKey.Random]);
                        var type = Convert.ToInt32(mapData[(byte)MapKey.Type]);
                        var x = Convert.ToInt32(mapData[(byte)MapKey.X]);
                        var y = Convert.ToInt32(mapData[(byte)MapKey.Y]);

                        Debug.LogFormat("map:number:{0}, random:{1}, type:{2}, x:{3} y:{4}", number, random, type, x,
                            y);
                        if (number == 0)
                            TerrainService.single.CreateTerrain(x, y, random, type);
                        else
                            TerrainService.single.CreateTerrain(number / 2, number / 2, random, type);
                        break;
                    case InfoKey.OpenMapKey:
                        var map = (string)detail[(byte)DetailKey.Value];
                        TerrainService.single.CreateTerrain(map);
                        break;
                }
            }
        }

        public void OnFinish(object result)
        {
        }

        public void OnSwitchArea()
        {
        }

        public void OnInstance(int targetIndex, byte relation, GamePlayer player, Vector3 position, bool isLocal,
            NetworkEsse master, Action<InitSyncListener> cb)
        {
        }

        public void OnDestroy(NetworkEsse instance, GamePlayer player)
        {
        }

        public void OnBroadcast(GamePlayer player, byte code, Vector3 position, StreamReader reader)
        {
        }

        public void OnEnterRoom(bool first)
        {
        }

        public void OnPlayerJoin(GamePlayer player)
        {
        }

        public void OnPlayerLeave(GamePlayer player)
        {
        }

        public void OnPlayerRejoin(GamePlayer player)
        {
        }

        public void OnFailConnect()
        {
        }

        public void OnReady()
        {
        }

        public void OnReadyPlayer(GamePlayer player)
        {
        }

        public void OnStart()
        {
        }

        public void OnReadyAll()
        {
        }

        public void OnInitGame()
        {
        }

        public void OnTimeMove(int startTime, int endTime, StreamReader reader, Vector3[] positions, int index,
            Vector3 middlePosition)
        {
        }

        public void OnTimeProcess(int startTime, int endTime, StreamReader reader)
        {
        }
    }
}